package homework.day09;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author ThinkBook
 */
//将学生按照成绩保存到集合中，并且名字叫tom的学生不管考多少分都位于班级的第一位。
public class Test07 {
    public static void main(String[] args) {
// 定义Set集合并传入一个自定义比较规则的Comparator
// 编写代码

        Comparator<Student> comp = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (!o1.getName().equals("tom")) {
                    if (o1.getScore() == o2.getScore()) {
                        return 0;
                    } else if (o1.getScore() > o2.getScore()) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
                return -1;
            }
        };
        Set<Student> set = new TreeSet<>(comp);
// 测试
        Student student1 = new Student("tom", 80);
        Student student2 = new Student("mike", 90);
        Student student3 = new Student("lily", 20);
        Student student4 = new Student("chris", 23);
        set.add(student3);
        set.add(student2);
        set.add(student1);
        set.add(student4);
        for (Student student : set) {
            System.out.println(student);
        }
    }
}
class Student {
    private String name;
    private int score;
    public Student(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", score=" + score +
                "]";
    }
}
